﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MotionCS.Controller;
using System.Threading;
using MotionCS.Helper;
using MotionCS.Enumerate;


namespace MotionCS.StatusLed
{
    
    /// <summary>
    /// 状态灯枚举
    /// </summary>
    public enum StateLedE
    {
        /// <summary>
        /// 全部关闭
        /// </summary>
        Off,
        /// <summary>
        /// 全部打开
        /// </summary>
        On,
        /// <summary>
        /// 红灯常亮
        /// </summary>
        RedOn,
        /// <summary>
        /// 绿灯常亮
        /// </summary>
        GreenOn,
        /// <summary>
        /// 黄灯常亮
        /// </summary>
        YellowOn,
        /// <summary>
        /// 红灯闪烁
        /// </summary>
        RedSlow,
        /// <summary>
        /// 绿灯闪烁
        /// </summary>
        GreenSlow,
        /// <summary>
        /// 黄灯闪烁
        /// </summary>
        YellowSlow,
        /// <summary>
        /// 红灯蜂鸣器常开
        /// </summary>
        RedBeepOn,
        /// <summary>
        /// 红灯闪烁蜂鸣器闪烁
        /// </summary>
        RedSlowBeepOn,
    }

    public class LedConfig
    {
        /// <summary>
        /// 模组key
        /// </summary>
        public int ModuleKey;
        /// <summary>
        /// 红灯
        /// </summary>
        public DoE Red;
        /// <summary>
        /// 绿灯
        /// </summary>
        public DoE Green;
        /// <summary>
        /// 黄灯
        /// </summary>
        public DoE Yellow;
        /// <summary>
        /// 蜂鸣器
        /// </summary>
        public DoE Beep;
    }
   
    public class LedControl
    {
        /// <summary>
        /// LED定义
        /// </summary>
        private enum LedE
        {
            /// <summary>
            /// 红灯
            /// </summary>
            Red,
            /// <summary>
            /// 绿灯
            /// </summary>
            Green,
            /// <summary>
            /// 黄灯
            /// </summary>
            Yellow,
            /// <summary>
            /// 蜂鸣器
            /// </summary>
            Beep,
            /// <summary>
            /// 数量
            /// </summary>
            Number
        }
        /// <summary>
        /// 模组Key
        /// </summary>
        public int ModuleKey { get => config.ModuleKey; }
        /// <summary>
        /// 是否启用
        /// </summary>
        private bool isEnable;
        public bool IsEnable { get => isEnable; }
        /// <summary>
        /// Led配置
        /// </summary>
        private LedConfig config;
        /// <summary>
        /// 状态灯集合
        /// </summary>
        private Dictionary<LedE, StatusLed> LedDic;
        /// <summary>
        /// 红灯
        /// </summary>
        private IDoBit DoRed;
        /// <summary>
        /// 绿灯
        /// </summary>
        private IDoBit DoGreen;
        /// <summary>
        /// 黄灯
        /// </summary>
        private IDoBit DoYellow;
        /// <summary>
        /// 蜂鸣器
        /// </summary>
        private IDoBit DoBeep;
        /// <summary>
        /// 当前状态
        /// </summary>
        private StateLedE curStatus;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="conf"></param>
        public LedControl(LedConfig conf) 
        { 
            config = conf;
            if (HardwareInit() != 0)
            {
                return;
            }
            isEnable = true;
        }
        /// <summary>
        /// 更新状态
        /// </summary>
        public void UpdateStatus()
        {
            LedDic.ToList().ForEach(itm =>
            {
                itm.Value.UpdateLedStatus();
            });
        }

        /// <summary>
        /// 模块初始化
        /// </summary>
        private int HardwareInit()
        {
            DoRed = MotionDevice.Instance.DoBit(config.Red);
            DoGreen = MotionDevice.Instance.DoBit(config.Green);
            DoYellow = MotionDevice.Instance.DoBit(config.Yellow);
            DoBeep = MotionDevice.Instance.DoBit(config.Beep);
            if (DoRed == null || DoGreen == null || DoYellow == null || DoBeep == null)
            {
                return -1;
            }

            LedDic = new Dictionary<LedE, StatusLed>()
            {
                {LedE.Red, new StatusLed((Int32)LedE.Red, SetLedState) },
                {LedE.Green, new StatusLed((Int32)LedE.Green, SetLedState) },
                {LedE.Yellow, new StatusLed((Int32)LedE.Yellow, SetLedState) },
                {LedE.Beep, new StatusLed((Int32)LedE.Beep, SetLedState) }
            };
            
            return 0;
        }
        /// <summary>
        /// 常亮
        /// </summary>
        private void StateOn(LedE led)
        {
            LedDic[led].SetPeriod(10, 10);
        }
        /// <summary>
        /// 常闭
        /// </summary>
        private void StateOff(LedE led)
        {
            LedDic[led].SetPeriod(10, 0);
        }
        /// <summary>
        /// 闪烁
        /// </summary>
        private void StateFlashSlow(LedE led)
        {
            LedDic[led].SetPeriod(200, 100);
        }
        /// <summary>
        /// 设置工作状态
        /// </summary>
        /// <param name="status"></param>
        /// 


        public void SetWorkStatus(StateLedE status)
        {
            if (status == curStatus)
            {
                return;
            }

            curStatus = status;
            switch (status)
            {
                case StateLedE.Off: // 灯关闭
                    StateOff(LedE.Red);
                    StateOff(LedE.Green);
                    StateOff(LedE.Yellow);
                    StateOff(LedE.Beep);
                    break;
                case StateLedE.On:
                    StateOn(LedE.Red);
                    StateOn(LedE.Green);
                    StateOn(LedE.Yellow);
                    StateOff(LedE.Beep);
                    break;
                case StateLedE.RedOn: // 红灯亮
                    StateOn(LedE.Red);
                    StateOff(LedE.Green);
                    StateOff(LedE.Yellow);
                    StateOff(LedE.Beep);
                    break;
                case StateLedE.GreenOn: // 绿灯亮
                     StateOff(LedE.Red);
                     StateOn(LedE.Green);
                    StateOff(LedE.Yellow);
                    StateOff(LedE.Beep);
                    break;
                case StateLedE.YellowOn: // 黄灯亮
                    StateOff(LedE.Red);
                    StateOff(LedE.Green);
                    StateOn(LedE.Yellow);
                    StateOff(LedE.Beep);
                    break;
                case StateLedE.RedSlow: // 红灯闪烁
                    StateFlashSlow(LedE.Red);
                    StateOff(LedE.Green);
                    StateOff(LedE.Yellow);
                    StateOff(LedE.Beep);
                    break;
                case StateLedE.GreenSlow: // 绿灯闪烁
                    StateOff(LedE.Red);
                    StateFlashSlow(LedE.Green);
                    StateOff(LedE.Yellow);
                    StateOff(LedE.Beep);
                    break;
                case StateLedE.YellowSlow: // 黄灯闪烁
                    StateOff(LedE.Red);
                    StateOff(LedE.Green);
                    StateFlashSlow(LedE.Yellow);
                    StateOff(LedE.Beep);
                    break;
                case StateLedE.RedBeepOn: // 红灯亮 蜂鸣器响
                    StateOn(LedE.Red);
                    StateOff(LedE.Green);
                    StateOff(LedE.Yellow);
                    StateOn(LedE.Beep);
                    break;
                case StateLedE.RedSlowBeepOn: // 红灯闪烁 蜂鸣器闪烁
                    StateFlashSlow(LedE.Red);
                    StateOff(LedE.Green);
                    StateOff(LedE.Yellow);
                    StateFlashSlow(LedE.Beep);
                  
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 关闭蜂鸣器.红灯                      
        /// </summary>
        public void CloseBeep()
        {
           StateOff(LedE.Red);
           StateOff(LedE.Beep);
    
        }

        public void OpenBeep()
        {
            StateFlashSlow(LedE.Red);
            StateFlashSlow(LedE.Beep);
        }

        /// <summary>
        /// 设置LED状态
        /// </summary>
        /// <param name="num"></param>
        /// <param name="state"></param>
        private void SetLedState(Int32 num, Int32 state)
        {
            switch ((LedE)num)
            {
                case LedE.Red:
                    DoRed.SetOut((uint)state);
                    break;
                case LedE.Green:
                    DoGreen.SetOut((uint)state);
                    break;
                case LedE.Yellow:
                    DoYellow.SetOut((uint)state);
                    break;
                case LedE.Beep:
                    DoBeep.SetOut((uint)state);
                    break;
            }
        }
    }
}
